package com.wenan.ui.controls.listpanel
{
	import com.wenan.ui.controls.listBoxes.CheckBoxColumn;
	import com.wenan.ui.controls.listBoxes.CheckBoxHeaderRender;
	import com.wenan.ui.controls.listBoxes.CheckBoxItemRenderer;
	import com.wenan.ui.controls.listBoxes.CheckListBoxItemEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.ClassFactory;
	import mx.events.ListEvent;
	import mx.states.SetStyle;
	
	[Event(name="itemSelected", type="com.wenan.ui.controls.listBoxes.CheckListBoxItemEvent")]
	public class SquareListBox2 extends VBox
	{
		private var titleLabel:Label;
		private var dataGrid:DataGridSpec;
		private var dataTemp:ArrayCollection;
		
		private var pageSize:int=3;
		private var pageno:int = 1;
		private var pageTotle:int;
		private var first:int;
		private var last:int;
		private var dataTotle:int;
		
		public function SquareListBox2()
		{
			super();
			
			this.width=130;
			setStyle("verticalGap", "0");
			setStyle("borderStyle", "solid");
			setStyle("borderColor", "#99a6c8");
			setStyle("cornerRadius","2");
			
			initilizeComponents();	
		}
		
		public function get title():String
		{
			return titleLabel.text;
		}
		
		public function set title(value:String):void
		{
			titleLabel.text = value;
		}
		
		[Bindable]
		public function get dataProvider():Object
		{
			return dataGrid.dataProvider;
		}
		
		public function set dataProvider(value:Object):void
		{
			dataGrid.dataProvider = value;
			dataTemp = value as ArrayCollection;
			dataTotle = dataTemp.length;
			
			if(pageSize<dataTotle)
				this.height=(pageSize+1)*27;
			else
				this.height=(dataTotle+1)*27;
		}
		
		public function get labelField():String
		{
			return dataGrid.columns[1].dataField;
		}
		
		public function set labelField(value:String):void
		{
			dataGrid.columns[1].dataField = value;
		}
		
		public function get selectedItem():Object
		{
			return dataGrid.selectedItem;
		}
		
		public function set selectedItem(value:Object):void
		{
			dataGrid.selectedItem = value;
		}
		
		public function get selectedItems():Array
		{
			return dataGrid.selectedItems;
		}
		
		public function set selectedItems(value:Array):void
		{
			dataGrid.selectedItems = value;
		}
		
		/**
		 * 初始化控件。
		 */
		private function initilizeComponents():void
		{
			var client:Canvas = new Canvas();
			client.percentWidth = 100;
			client.percentHeight = 100;
			client.styleName = "GradientColor";
			
			var clientBox:VBox = new VBox();
			clientBox.percentWidth = 100;
			clientBox.percentHeight = 100;
			clientBox.setStyle("verticalGap", "0");
			
			initilizeClientBox(clientBox);
			
			client.addChild(clientBox);
			
			addChild(client);
			
			var title:HBox = new HBox();
			title.percentWidth = 100;
			title.height = 24;
			title.setStyle("verticalAlign", "middle");
			title.setStyle("paddingLeft", "8");
			title.styleName = "RepeatBackGround";
			
			titleLabel = new Label();
			title.addChild(titleLabel);
			
			addChild(title);
		}
		
		private function initilizeClientBox(clientBox:VBox):void
		{
			if(pageSize<dataTotle)
			{
				var scroller:HBox = new HBox();
				scroller.percentWidth = 100;
				scroller.height = 18;
				scroller.setStyle("verticalAlign", "middle");
				scroller.setStyle("horizontalAlign", "center");
				
				var upButton:Button = new Button();
				upButton.styleName = "UpButton";
				upButton.addEventListener(MouseEvent.CLICK, upClickHander);
				scroller.addChild(upButton);
				
				var downButton:Button = new Button();
				downButton.styleName = "DownButton";
				downButton.addEventListener(MouseEvent.CLICK, downClickHander);
				scroller.addChild(downButton);
				
				clientBox.addChild(scroller);
			}
			
			dataGrid = new DataGridSpec();
			dataGrid.percentWidth = 100;
			dataGrid.percentHeight = 100;
			dataGrid.showHeaders = false;
			dataGrid.setStyle("verticalGridLineColor", "0xD0D4D7");
			dataGrid.verticalScrollPolicy = "off";
			dataGrid.columns = buildDataGridColumns();
			dataGrid.addEventListener(ListEvent.ITEM_CLICK,itemClick_Handler);
			dataGrid.addEventListener(ListEvent.ITEM_ROLL_OVER,itemOver_Handler);
			dataGrid.addEventListener(ListEvent.ITEM_ROLL_OUT,itemOut_Handler);
			clientBox.addChild(dataGrid);
			
		}
		
		private function buildDataGridColumns():Array
		{
			var columns:Array = [];
			
			var nameColumn:DataGridColumn = new DataGridColumn();
			nameColumn.dataField = "orgname";
			columns.push(nameColumn);
			
			return columns;
		}
		
		private function itemSelectedHandler(event:Event):void 
		{
			var obj:Object = event;
			dispatchEvent(new CheckListBoxItemEvent(CheckListBoxItemEvent.ITEM_SELECTED, obj.item));
		}
		
		private function upClickHander(event:MouseEvent):void
		{
			pageSize = dataGrid.rowCount;
			if (pageno > 1) {
				pageno--;
				changeDataProvider();
			}
		}
		
		private function downClickHander(event:MouseEvent):void
		{
			pageSize = dataGrid.rowCount;
			pageTotle = (dataTotle + pageSize - 1) / pageSize;
			if (pageno < pageTotle) {
				pageno++;
				changeDataProvider();
			}
		}
		
		private function changeDataProvider():void 
		{
			first = (pageno - 1) * pageSize;
			last = pageno * pageSize -1;
			var temp:ArrayCollection = new ArrayCollection();
			for (var i:int=first; i <dataTotle && i<=last; i++){
				temp.addItem(dataTemp.getItemAt(i));
			}
			dataGrid.dataProvider = temp;
		}
		
		/**
		 * 鼠标点击广场列表项处理方法  
		 * 
		 * @param event 事件对象
		 * */
		private function itemClick_Handler(event:ListEvent):void
		{
			dispatchEvent(new ListItemEvent(ListItemEvent.LIST_ITEM_CLICK,event.itemRenderer.data));
		}
		
		/**
		 * 鼠标经过广场列表项处理方法
		 * 
		 * @param event 事件对象
		 * */
		private function itemOver_Handler(event:ListEvent):void
		{
			dispatchEvent(new ListItemEvent(ListItemEvent.LIST_ITEM_OVER,event.itemRenderer.data));
		}
		
		/**
		 * 鼠标离开广场列表项处理方法
		 * 
		 * @param event 事件对象
		 * */
		private function itemOut_Handler(event:ListEvent):void
		{
			dispatchEvent(new ListItemEvent(ListItemEvent.LIST_ITEM_OUT,event.itemRenderer.data));
		}
	}
}